package com.mvc.model;

import java.io.File;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.jfinal.core.JFinal;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.mvc.request.ReqVenueAdd;
import com.mvc.request.ReqVenueUpdate;
import com.mvc.response.RspDead;

public class Venue extends Model<Venue> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 740852019763104756L;

	/***
	 * 表字段
	 * 
	 */
	public static interface TableField {
		String ID = "id";
		String USERID = "userId";
		String GRADE = "grade";
		String TYPE = "type";
		String MONEY = "money";
		String STYLEID = "styleId";
		String NAME = "name";
		String WORSHIPID = "worshipId";
		String RELATIONSHIP = "relationship";
		String DESCNBE = "descnbe";
		String CREATETIME = "createTime";
		String WORSHIPSIZE = "worshipSize";
	}

	public static final Venue dao = new Venue();

	public boolean add(final ReqVenueAdd req) {

		return Db.tx(new IAtom() {

			@Override
			public boolean run() throws SQLException {
				Venue venue = new Venue();
				venue.set(TableField.CREATETIME, new Date());
				venue.set(TableField.USERID, req.getUserId());
				venue.set(TableField.TYPE, req.getType());
				venue.set(TableField.RELATIONSHIP, req.getRelationship());
				venue.set(TableField.DESCNBE, req.getEitaph());
				venue.set(TableField.NAME, req.getName());
				venue.set(TableField.WORSHIPID, req.getWorshipId());
				venue.set(TableField.STYLEID, req.getStyleId());
				venue.set(TableField.MONEY, 1000);
				venue.set(TableField.GRADE, 1);
				boolean isSucce = venue.save();
				if (isSucce) {

					for (RspDead rsp : req.getDead()) {
						Dead objDead = new Dead();
						objDead.set(Dead.TableField.NAME, rsp.getName());
						objDead.set(Dead.TableField.SEX, rsp.getSex());
						objDead.set(Dead.TableField.BRITHDAY, rsp.getBrithday());
						objDead.set(Dead.TableField.DEATH, rsp.getDeath());
						objDead.set(Dead.TableField.BRAND, rsp.getBrand());
						objDead.set(Dead.TableField.HEAD, rsp.getHead());
						objDead.set(Dead.TableField.VENUEID,
								venue.getInt(Venue.TableField.ID));
						isSucce &= objDead.save();
					}
				}
				return isSucce;
			}
		});
	}

	public boolean edit(final Venue venue, final ReqVenueUpdate req) {

		return Db.tx(new IAtom() {

			@Override
			public boolean run() throws SQLException {
				venue.set(TableField.USERID, req.getUserId());
				venue.set(TableField.TYPE, req.getType());
				venue.set(TableField.RELATIONSHIP, req.getRelationship());
				venue.set(TableField.DESCNBE, req.getEitaph());
				venue.set(TableField.NAME, req.getName());
				venue.set(TableField.WORSHIPID, req.getWorshipId());
				venue.set(TableField.STYLEID, req.getStyleId());

				boolean isSucce = venue.update();
				if (isSucce) {

					for (RspDead rsp : req.getDead()) {
						Dead objDead = Dead.dao.findById(rsp.getId());
						if (objDead == null) {
							isSucce &= false;
							break;
						}

						objDead.set(Dead.TableField.NAME, rsp.getName());
						objDead.set(Dead.TableField.SEX, rsp.getSex());
						objDead.set(Dead.TableField.BRITHDAY, rsp.getBrithday());
						objDead.set(Dead.TableField.DEATH, rsp.getDeath());
						objDead.set(Dead.TableField.BRAND, rsp.getBrand());
						objDead.set(Dead.TableField.HEAD, rsp.getHead());
						isSucce &= objDead.update();
					}
				}
				return isSucce;
			}
		});
	}

	public boolean delete(final int venueId) {

		return Db.tx(new IAtom() {

			@Override
			public boolean run() throws SQLException {

				// 1.删除场馆
				boolean success = dao.deleteById(venueId);
				List<Dead> deads = Dead.dao.find(
						"select * from dead where venueId=?", venueId);
				// 2.删除人员
				success &= Db.update("delete from dead where venueId = ?",
						venueId) >= 0;
				// 4.删除祭拜记录[物品记录]
				success &= Db.update("delete from worship where venueId = ?",
						venueId) >= 0;
				// 5.删除留言
				success &= Db.update("delete from message where venueId = ?",
						venueId) >= 0;

				if (deads != null) {
					for (Dead dead : deads) {
						String head = dead.get(Dead.TableField.HEAD);
						if (StrKit.notBlank(head)) {
							// 用户设置的默认上传目录
							String saveDir = JFinal.me().getConstants()
									.getBaseUploadPath();
							File file = new File(saveDir + File.separator
									+ head);
							if (file.exists() && file.isFile()) {
								file.delete();

							}
						}
					}
				}
				return success;
			}
		});
	}

	/**
	 * 关联逝者表
	 * 
	 * @return
	 */
	public List<Dead> getDeadList() {
		return Dead.dao.find("select * from dead where venueId=?",
				getInt(TableField.ID));
	}

	/**
	 * 主页 关联风格表
	 */
	public Style getStyle() {
		return Style.dao.findById(getInt(TableField.STYLEID));
	}

	/**
	 * 大厅 关联风格表
	 */
	public Style getWorshipStyle() {
		return Style.dao.findById(getInt(TableField.WORSHIPID));
	}

	/***
	 * 获取最新创建列表
	 * 
	 * @param req
	 * @return
	 */
	public Page<Venue> queryNewPage(int pageNumber, int pageSize) {

		String select = "select v.* ";
		String formwhere = "from venue v where 1=1 order by v.createTime desc ";
		return dao.paginate(pageNumber, pageSize, select, formwhere);
	}

	/***
	 * 获取祭拜次数最多列表
	 * 
	 * @param req
	 * @return
	 */
	public Page<Venue> queryWorshipPage(int pageNumber, int pageSize) {
		String select = "select v.* ";
		String formwhere = "from venue v where 1=1 order by v.worshipSize desc ";
		return dao.paginate(pageNumber, pageSize, select, formwhere);

	}

	/***
	 * 获取排名列表
	 * 
	 * @param req
	 * @return
	 */
	public Page<Venue> queryRankingPage(int pageNumber, int pageSize) {
		String select = "select v.* ";
		String formwhere = "from venue v where 1=1  order by v.money desc ";
		return dao.paginate(pageNumber, pageSize, select, formwhere);

	}

	/***
	 * 获取用戶創建的場館列表
	 * 
	 * @param req
	 * @return
	 */
	public List<Venue> queryUserPage(int userId) {
		String select = "select v.* from venue v where 1=1 and v.userId=? order by v.createTime desc ";
		return dao.find(select, userId);

	}
}
